/**
 * 原型拓展内容
 */


 /**
  * Spawn
  * ===============================================================================================
  * 该拓展为Spawn生成Creep新增了任务池特性，Spawn将会按任务列表依次生产Creep
  */
if(Memory.Spawn == undefined){//初始化
    Memory.Spawn = {
        pool:{}
    };
}
Spawn.prototype.spawnInPool = function(body,name,opts){

    //判断有无该Spawn的任务池
    if(Memory.Spawn.pool[this.id] == undefined){
        Memory.Spawn.pool[this.id] = [];
    }

    //封装数据
    let pack = {
        body:body,
        name:name,
        opts:opts,
    }
    Memory.Spawn.pool[this.id].push(pack);

    return OK;
}

Spawn.prototype.displayPool = function(){

    //判断有无该Spawn的任务池
    if(Memory.Spawn.pool[this.id] == undefined){
        Memory.Spawn.pool[this.id] = [];
    }

    let px = this.pos.x+1;
    let py = this.pos.y;
    for(let item of Memory.Spawn.pool[this.id]){
        new RoomVisual(this.pos.room).text(item.name,px,py);
        py++;
    }
   
}

//出池 供自己调用取出数据
Spawn.prototype.popSpawn = function(){
    //判断有无该Spawn的任务池
    if(Memory.Spawn.pool[this.id] == undefined){
        Memory.Spawn.pool[this.id] = [];
    }

    //获取信息
    let pool = Memory.Spawn.pool[this.id];
    return pool.shift();//object or undefined
}

Spawn.prototype.flashPool = function(){
    //判断是否处于生产中
    let spawning = this.spawning;
    if(spawning == null){
        //进行生产
        let pop = this.popSpawn();
        if(pop != undefined){
            let {body,name,opts} = pop;
            this.spawnCreep(body,name,opts);
            return this.spawning;
        }
        return null;
    }else{
        return this.spawning;
    }
}

Spawn.prototype.isExistInPool = function(name){
    //判断有无该Spawn的任务池
    if(Memory.Spawn.pool[this.id] == undefined){
        Memory.Spawn.pool[this.id] = [];
        return false;
    }

    for(let item of Memory.Spawn.pool[this.id]){
        let {name1} = item;
        if(name1 == name) return true;
    }

    return false;
}